package com.android.kotlincoroutinemvvmdemo.event
import android.app.Activity
import android.content.Context
import android.widget.TextView
import androidx.databinding.BindingAdapter
import androidx.fragment.app.FragmentActivity
import com.android.kotlincoroutinemvvmdemo.AirBubblesViewActivity
import com.android.kotlincoroutinemvvmdemo.BezierCubicToActivity
import com.android.kotlincoroutinemvvmdemo.BezierQuadToActivity
import com.android.kotlincoroutinemvvmdemo.Constants
import com.android.kotlincoroutinemvvmdemo.ContentProviderActivity
import com.android.kotlincoroutinemvvmdemo.CurveLinesActivity
import com.android.kotlincoroutinemvvmdemo.DayNightModeActivity
import com.android.kotlincoroutinemvvmdemo.DefineView2Activity
import com.android.kotlincoroutinemvvmdemo.DefineView3Activity
import com.android.kotlincoroutinemvvmdemo.DefineView4Activity
import com.android.kotlincoroutinemvvmdemo.DefineView5Activity
import com.android.kotlincoroutinemvvmdemo.DefineViewActivity
import com.android.kotlincoroutinemvvmdemo.DoubleBindingActivity
import com.android.kotlincoroutinemvvmdemo.FlingTestActivity
import com.android.kotlincoroutinemvvmdemo.FlowCaseActivity
import com.android.kotlincoroutinemvvmdemo.FlowDemoActivity
import com.android.kotlincoroutinemvvmdemo.GlideCaseActivity
import com.android.kotlincoroutinemvvmdemo.R
import com.android.kotlincoroutinemvvmdemo.RecyclerViewPoolActivity
import com.android.kotlincoroutinemvvmdemo.ReflectActivity
import com.android.kotlincoroutinemvvmdemo.UnspecifiedActivity
import com.android.kotlincoroutinemvvmdemo.WebViewActivity
import com.android.kotlincoroutinemvvmdemo.adapter.OneFiAdapter
import com.android.kotlincoroutinemvvmdemo.adapter.TwoFiAdapter
import com.android.kotlincoroutinemvvmdemo.bean.OneFi
import com.android.kotlincoroutinemvvmdemo.dialog.BlurEffectDialog
import com.android.kotlincoroutinemvvmdemo.dialog.TestDayNightDialog
import com.android.kotlincoroutinemvvmdemo.dialog.TestDayNightDialogFragment
import com.android.kotlincoroutinemvvmdemo.dialog.TestDayNightDialogFragment2
import com.android.kotlincoroutinemvvmdemo.dialog.TestDialogContent
import com.android.kotlincoroutinemvvmdemo.showToast
import com.android.kotlincoroutinemvvmdemo.startActivity
import com.android.kotlincoroutinemvvmdemo.util.FastBlurUtil

class ItemViewEvent {

    private var mContext: Context
    private lateinit var mAdapter: OneFiAdapter
    private lateinit var mTwoAdapter: TwoFiAdapter


    constructor(context: Context, adapter: OneFiAdapter) {
        mContext = context
        mAdapter = adapter
    }

    constructor(context: Context, adapter: TwoFiAdapter) {
        mContext = context
        mTwoAdapter = adapter
    }

    private var mTestDayNightDialog: TestDayNightDialog? = null

    private var mTestDialogContent: TestDialogContent? = null

    private var mBlurEffectDialog: BlurEffectDialog? = null

    fun clickOneFiItem(oneFi: OneFi) {
        mContext.showToast("content:${oneFi.content},position:${oneFi.itemPosition}")
        //mAdapter.removeDataItem(oneFi, oneFi.itemPosition)

        //val newOneFi = OneFi("小米MIUI首个内测版发布")
        //mAdapter.addDataItem(newOneFi, 100)

        when (oneFi.itemPosition) {
            0 -> {
                mContext.startActivity<DoubleBindingActivity>()
            }

            1 -> {
                mContext.startActivity<CurveLinesActivity>()
            }

            2 -> {
//                if (mTestDayNightDialog == null) {
//                    mTestDayNightDialog = TestDayNightDialog(mContext)
//                }
//                mTestDayNightDialog?.show()

                if (mBlurEffectDialog == null) {
                    val bmp = FastBlurUtil.getBlurBackgroundDrawer(mContext as Activity, 3F)
                    mBlurEffectDialog = BlurEffectDialog(mContext, bmp)
                }
                mBlurEffectDialog?.show()
            }

            3 -> {
                mContext.startActivity<DayNightModeActivity>()
            }

            4 -> {
                mAdapter.notifyItemChanged(0, Constants.CHANGE_CONTENT)

                if (mTestDialogContent == null) {
                    mTestDialogContent = TestDialogContent(mContext)
                }
                mTestDialogContent?.show()
            }

            5 -> {
                mContext.startActivity<FlowDemoActivity>()
            }

            6 -> {
                mContext.startActivity<FlowCaseActivity>()
            }

            7 -> {
                mContext.startActivity<ContentProviderActivity>()
            }

            8 -> {
                mContext.startActivity<RecyclerViewPoolActivity>()
            }

            9 -> {
                mContext.startActivity<FlingTestActivity>()
            }

            10 -> {
                mContext.startActivity<AirBubblesViewActivity>()
            }

            11 -> {
                mContext.startActivity<ReflectActivity>()
            }

            12 -> {
                mContext.startActivity<WebViewActivity>()
            }

            13 -> {
                mContext.startActivity<DefineViewActivity>()
            }

            14 -> {
                mContext.startActivity<GlideCaseActivity>()
            }

            15 -> {
                mContext.startActivity<DefineView2Activity>()
            }

            16 -> {
                mContext.startActivity<BezierQuadToActivity>()
            }

            17 -> {
                mContext.startActivity<BezierCubicToActivity>()
            }

            18 -> {
                mContext.startActivity<DefineView3Activity>()
            }

            19 -> {
                if (mContext is FragmentActivity) {
                    val dialogFragment = TestDayNightDialogFragment.newInstance()
                    dialogFragment.show((mContext as FragmentActivity).supportFragmentManager,"dialogFragment")
                }
            }

            20 -> {
                if (mContext is FragmentActivity) {
                    val dialogFragment2 = TestDayNightDialogFragment2.newInstance()
                    dialogFragment2.show((mContext as FragmentActivity).supportFragmentManager,"dialogFragment2")
                }
            }

            21 -> {
                mContext.startActivity<UnspecifiedActivity>()
            }

            22 -> {
                mContext.startActivity<DefineView4Activity>()
            }
            23 -> {
                mContext.startActivity<DefineView5Activity>()
            }

            else -> {

            }
        }
    }

    companion object {
        private const val TEXT_COLOR_MODE = 6
        //@BindingAdapter标识的方法必须是static的
        //BindingAdapter和BindingConversion都是需是静态方法,
        //但是由于BindingConversion必须在静态类中,不能放在静态模块中.
        @JvmStatic
        //@BindingAdapter(value = ["app:updateTextColor"], requireAll = false)
        @BindingAdapter(value = ["updateTextColor"], requireAll = false)
        fun TextView.updateTextColor(itemPosition: Int) {
            when (itemPosition % TEXT_COLOR_MODE) {
                0 -> {
                    setTextColor(resources.getColor(R.color.color_64C630))
                }
                1-> {
                    setTextColor(resources.getColor(R.color.color_B128C9))
                }
                2 -> {
                    setTextColor(resources.getColor(R.color.color_da8989))
                }
                3 -> {
                    setTextColor(resources.getColor(R.color.color_ffeb3b))
                }
                4 -> {
                    setTextColor(resources.getColor(R.color.purple_700))
                }
                else -> {
                    setTextColor(resources.getColor(R.color.color_d05722))
                }
            }
        }
    }

}